{% extends "base.html" %}

{% block content %}
<div class="page-title">
    <h3>在下方输入要删除的员工的编号：</h3>
</div>
<div class="form-container">
    <form id="delete-employee-form">
        <label for="employee_id">员工编号：</label>
        <input type="text" id="employee_id" name="employee_id" class="input-field" required><br>
        <div class="button-container">
            <button type="submit" class="login-button">删除</button>
            <button type="reset" class="login-button" style="background-color: grey;">重置</button>
        </div>
    </form>
</div>

<script>
    document.getElementById('delete-employee-form').addEventListener('submit', function(event) {
        event.preventDefault();

        var employeeId = document.getElementById('employee_id').value;
        if (employeeId) {
            fetch(`/api/employees/${employeeId}`, {
                method: 'GET',
                headers: {
                    'Content-Type': 'application/json'
                }
            }).then(response => {
                if (response.ok) {
                    if (confirm('是否确定删除？')) {
                        fetch(`/api/employees/${employeeId}`, {
                            method: 'DELETE',
                            headers: {
                                'Content-Type': 'application/json'
                            }
                        }).then(response => {
                            if (response.ok) {
                                alert('员工删除成功');
                                this.reset();
                            } else {
                                response.json().then(data => {
                                    alert('删除员工失败: ' + data.error);
                                });
                            }
                        }).catch(error => {
                            console.error('Error:', error);
                            alert('删除员工失败');
                        });
                    }
                } else {
                    response.json().then(data => {
                        alert('该员工编号不存在');
                    });
                }
            }).catch(error => {
                console.error('Error:', error);
                alert('删除员工失败');
            });
        } else {
            alert('请输入员工编号');
        }
    });

    document.querySelector('button[type="reset"]').addEventListener('click', function(event) {
        if (!confirm('是否确定重置已输入信息？')) {
            event.preventDefault();
        }
    });
</script>
{% endblock %}
